<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');

$error = FALSE;
$location_id = $_SESSION['checkin']['location_id'];
if($_POST) {
    $route_id = $routeMgr->add($location_id, $_POST, $_SESSION['user']['id']);
    if($route_id && $location_id) {
        $_SESSION['msg'] = $msgMgr->get('add-route-success');
        header('Location: /viewroute.php?id=' . $route_id);
        exit();
    } else {
        $error = $msgMgr->get('add-route-error');
        $is_short = (isset($_POST['is_short']) ? 'checked' : '');
        $rope = $_POST['rope'];
        $name = $_POST['name'];
        $date = $_POST['date'];
    }
}

$difficulties = $diffMgr->getAll();
$difficulty_select = array('<select name="difficulty_id">');
foreach($difficulties as $row) {
    $difficulty_select[] =
        '<option
        value="' . $row['id'] . '"' .
        (isset($_POST['difficulty_id']) && $_POST['difficulty_id'] == $row['id'] ? 'selected' : '') .
        '>' .
        $row['name'] .
        '</option>';
}
$difficulty_select[] = '</select>';

$colours = $colourMgr->getAll();
$colour_select = array('<select name="colour_id">');
foreach($colours as $row) {
    $colour_select[] =
        '<option
        value="' . $row['id'] . '"' .
        (isset($_POST['colour_id']) && $_POST['colour_id'] == $row['id'] ? 'selected' : '') .
        '>' .
        $row['name'] .
        '</option>';
}
$colour_select[] = '</select>';

$location_id = $_SESSION['checkin']['location_id'];
$authors = $authorMgr->getAll($location_id);
$author_select = array('<select name="author_id">', '<option value=""></option>');
foreach($authors as $row) {
    $author_select[] =
        '<option
        value="' . $row['id'] . '"' .
        (isset($_POST['author_id']) && $_POST['author_id'] == $row['id'] ? 'selected' : '') .
        '>' .
        $row['name'] .
        '</option>';
}
$author_select[] = '</select>';

$types = $routeMgr->getTypes();
$type_select = array('<select name="type_id">');
foreach($types as $row) {
   $type_select[] =
      '<option
      data-type="' . $row['type'] . '"
      value="'. $row['id'] . '"' .
      (isset($_POST['type_id']) && $_POST['type_id'] == $row['id'] ? 'selected' : '') .
      '>' .
      ($row['type'] ? ucfirst($langMgr->get('routetype_' . $row['type'])) : '') .
      '</option>';
}
$type_select[] = '</select>';

$replaces = array(
    'difficulty_select'     => implode("\n", $difficulty_select),
    'colour_select'         => implode("\n", $colour_select),
    'is_short'              => (isset($is_short) ? $is_short : ''),
    'author_select'         => implode("\n", $author_select),
    'type_select'           => implode("\n", $type_select),
    'rope'                  => (isset($rope) ? $rope : ''),
    'name'                  => (isset($name) ? $name : ''),
    'date'                  => (isset($date) ? $date : ''),
    'error'                 => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_add_route'  => $langMgr->get('header_add_route'),
    'text_difficulty'   => $langMgr->get('text_difficulty'),
    'text_colour'       => $langMgr->get('text_colour'),
    'text_routetype'    => $langMgr->get('text_routetype'),
    'text_rope'         => $langMgr->get('text_rope'),
    'text_author'       => $langMgr->get('text_author'),
    'text_comment'      => $langMgr->get('text_comment'),
    'text_datecreated'  => $langMgr->get('text_datecreated'),
    'text_route_short'   => $langMgr->get('text_route_short'),
    'btn_add_route'     => $langMgr->get('btn_add_route'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
